<?php

/**
 * 通用控制器
 * @author:Cloud
 * @copyright:TcitCMS
 * @since:2014-05-12
 */

namespace Home\Controller;

class CommonController extends \Think\Controller
{

    protected $UserInfo;
    protected $menu_id;
    protected $lm_id;
    protected $map;
    protected $order;
    protected $pagesize;
    protected $url;

    function _initialize()
    {
        $settingModel = new \Common\Model\SettingModel();
        $settingData = $settingModel->select();
        $_data = array();
        foreach ($settingData as $value) {
            $_data[$value['key']] = $value['value'];
        }
        $settingData = $_data;
        unset($_data);
        $this->assign('settingData', $settingData);
        if ($this->lm_id) {
            $menuModel = new \Common\Model\MenuModel();
            $seoData = $menuModel->where('`lm_id`=' . $this->lm_id)->getField('lm_id,seo_title,seo_keywords,seo_description');
            $this->assign('seoData', $seoData[$this->lm_id]);
        }
        $model = new \Common\Model\NewsModel();
        $cateModel = new \Common\Model\CategoryModel();
        //首页幻灯
        $indexBannerData = $model->where('`lm_id`=1 and `deleted`=0 and smallpic != "" ')->order('sort')->select();
        $this->assign('indexBannerData', $indexBannerData);
        //右侧文章列表
        $indexSaleData = $model->where('`lm_id`=6 and `deleted`=0')->limit(9)->order('created desc')->select();
        $this->handles($indexSaleData, 'title', 14);

        $memberModel = new \Common\Model\MemberModel();
        $userData = $memberModel->checkLogin();
        if ($userData) {
            $this->UserInfo = $userData;
            $this->assign('UserInfo', $this->UserInfo);
        }
        $this->assign('indexSaleData', $indexSaleData);
    }

    function indexInitialize()
    {
        $map = array();
        $map['deleted'] = array('EQ', 0);
        $map['lm_id'] = array('EQ', $this->lm_id);
        $this->map = $map;
        $this->order = 'created desc';
        $this->pagesize = C('DEFAULT_PAGE_SIZE_10');
        $this->url = U(CONTROLLER_NAME . '/index');
    }

    function commonIndex($model = '')
    {
        empty($model) && $this->error('模型不能为空');
        $url = $this->url ? $this->url : U(CONTROLLER_NAME . '/index');
        $pageNo = I('get.PageNo', '1', 'intval');
        $pageNum = ($pageNo - 1) * $this->pagesize;
        $limit = "{$pageNum},{$this->pagesize}";

        $count = $model->where($this->map)->count();

        $pager = new \Common\Org\Pager();
        $pagerData = $pager->getPagerData($count, $pageNo, $url, 2, $this->pagesize); //参数记录数 当前页数 链接地址 显示样式 每页数量

        $data = $model->where($this->map)->order($this->order)->limit($limit)->select();

        $this->assign('data', $data);
        $this->assign('pagerData', $pagerData);

        $this->display();
    }

    function commonDetail($model = '')
    {
        empty($model) && $this->error('模型不能为空');
        $id = I('get.id', '', 'intval');
        !$id && $this->error('参数错误!');
        $data = $model->find($id);
        if ($data['deleted'] != 0)
            $this->error('纪录不存在!', '/');
        $this->assign('data', $data);
        $this->display();
    }

    //处理HTML标签,并转换成HTML实体,并截取指定长度
    function handle($str, $length)
    {
        return $length ? \Common\Org\Util::csubstr(strip_tags(htmlspecialchars_decode($str)), 0, $length) : htmlspecialchars_decode($str);
    }

    function handles(&$data, $field, $length)
    {
        foreach ($data as $k => $v) {
            $data[$k][$field] = $v[$field] && $length ? $this->handle($v[$field], $length) : '';
        }
        return;
    }

    function setDefaults(&$data, $field)
    {
        foreach ($data as $k => $v) {
            $data[$k][$field] = number_format($data[$k][$field], 2, '.', '') > 0 ? '¥' . number_format($data[$k][$field], 2, '.', '') : '免费';
        }
        return;
    }

    /**
     * 单图片上传字段处理
     * @param type $data
     * @param type $field
     * @param type $tags
     * @return type
     */
    function singleUpload(&$data, $field = array(), $tags = array())
    {
        foreach ($field as $f) {
            foreach ($tags as $g) {
                $temp = str_replace('.', '_' . $g . '.', $data[$f]);
                $data[$f . '_' . $g] = is_file(APP_ROOT . $temp) ? $temp : $data[$f];
            }
        }
        return;
    }

    /**
     * 多图片上传字段处理
     * @param type $data
     * @param type $tags
     * @return type
     */
    function multiUpload(&$data, $tags = array())
    {
        $picArray = explode("\n", $data['multipic']);
        foreach ($picArray as $k => $v) {
            $picArray[$k] = explode('|', $v);
            if ($picArray[$k][3] == 1)
                $default = $picArray[$k][1];
            foreach ($tags as $g) {
                $temp = str_replace('.', '_' . $g . '.', $picArray[$k][1]);
                $picArray[$k][$g] = is_file(APP_ROOT . $temp) ? $temp : $picArray[$k][1];
            }
        }
        $data['multipic'] = $picArray;
        if ($default)
            $data['default'] = $default;
        else
            $data['default'] = 1;
        foreach ($tags as $g) {
            $temp = str_replace('.', '_' . $g . '.', $data['default']);
            $data['default_' . $g] = is_file(APP_ROOT . $temp) ? $temp : $data['default'];
        }
        return;
    }

    /**
     * 特殊字段处理
     */
    protected function arrayMethod($data, $field, $record = TRUE)
    {
        if ($record) {
            foreach ($data as $k => $v) {
                $data[$k][$field] = explode('|', $data[$k][$field]);
            }
        } else {
            $data[$field] = explode('|', $data[$field]);
        }
        return;
    }

    /**
     * 默认图片处理
     */
    protected function imgDefault($data, $fieldArray, $record = TRUE)
    {
        if ($record) {
            foreach ($data as $k => $v) {
                foreach ($fieldArray as $field) {
                    $data[$k][$field] = $v[$field] ? $v[$field] : C('DEFAULT_IMAGE_DOC');
                }
            }
        } else {
            foreach ($fieldArray as $field) {
                $data[$field] = $v[$field] ? $data[$field] : C('DEFAULT_IMAGE_DOC');
            }
        }
        return;
    }

    /*
     * 关联字段处理
     */

    protected function arrayMethod1($data, $field, $lm_id, $record = TRUE)
    {
        if ($record) {
            foreach ($data as $k => $v) {
                $model = new \Common\Model\NewsModel();
                $tempData = $model->where('`lm_id`=' . $lm_id . ' and id=' . $v[$field])->find();
                $data[$k][$field] = $tempData['title'];
            }
        } else {
            $model = new \Common\Model\NewsModel();
            $tempData = $model->where('`lm_id`=' . $lm_id . ' and id=' . $data[$field])->find();
            $data[$field] = $tempData['title'];
        }
        return;
    }

    /**
     * 图片集中处理
     * @param type $data 记录(可以是单记录，也可以是记录集合)
     * @param type $field 要处理的上传字段
     * @param type $tags 要出来的缩略图标签
     * @param type $record 是否是数据集?bool
     * @return type
     */
    function picMethod(&$data, $field = array(), $tags = array(), $record = TRUE)
    {
        if ($record) {
            foreach ($data as $k => $v) {
                $this->singleUpload($v, $field, $tags);
                $this->multiUpload($v, $tags);
                $data[$k] = $v;
            }
        } else {
            $this->singleUpload($data, $field, $tags);
            $this->multiUpload($data, $tags);
        }
        return;
    }

    public function _empty()
    {
        header("HTTP/1.0  404  Not Found");
        $this->display('Common/empty');
    }

}

?>